package com.toune.movie.mvp.activity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.toune.movie.R;
import com.toune.movie.base.Api;
import com.toune.movie.base.AppConstant;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.myUtil.GetTimeUtils;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.base.myUtil.ProgressWebview;
import com.toune.movie.mvp.contract.PlayVideoContract;
import com.toune.movie.mvp.presenter.PlayVideoPresenter;
import com.vondear.rxtool.RxDeviceTool;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/15.
 */

public class PlayVideoActivity extends BaseActivity<PlayVideoPresenter> implements PlayVideoContract.View {
    @BindView(R.id.webview)
    ProgressWebview webview;
    //    @BindView(R.id.player_view)
//    IjkPlayerView playerView;
    private Uri mUri;
    private String htmlUrl;

    @Override // 实例化presenter
    public PlayVideoPresenter initPresenter() {
        return new PlayVideoPresenter();
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    private String playUrl;

    @Override
    public void init(Bundle savedInstanceState) {
        titleStr = getIntent().getStringExtra("title");
        htmlUrl = getIntent().getStringExtra("url");
        AVObject history = new AVObject(AppConstant.TABLE_HIS);
        history.put("title", titleStr);
        history.put("url", htmlUrl);
        history.put("time", GetTimeUtils.dateFormat());
        history.put("tag", 0);
        if (AVUser.getCurrentUser() != null) {
            history.put("userId", AVUser.getCurrentUser().getObjectId());
        }
        history.put("phoneId", RxDeviceTool.getUniqueSerialNumber());
        history.saveInBackground();
        mPresenter.getPlayUrl(htmlUrl);
        MyUtil.showADDialog(context);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_play_video;
    }

    @Override
    protected String getTitleStr() {
        return titleStr;
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            mUri = Uri.parse(playUrl);
//            playerView.init()
//                    .setVideoPath(mUri)
//                    .setMediaQuality(IjkPlayerView.MEDIA_QUALITY_HIGH)
//                    .enableDanmaku()
//                    .start();
            webview.loadUrl(playUrl);
        }
    };

    @Override
    public void setPlayUrl(final String url) {
        playUrl = url;
        handler.sendEmptyMessage(0);
    }
}
